package com.base_data_handler.function.air;

import java.io.File;
import java.io.FileInputStream;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Logger;

import org.apache.commons.dbutils.QueryRunner;

import com.alibaba.fastjson.JSONArray;
import com.base_data_handler.function.model.AirInfo;
import com.base_data_handler.utils.DataSourceUtils;
import com.base_data_handler.utils.NetData;
import com.base_data_handler.utils.PinyinUtil;
import com.base_data_handler.utils.StringUtils;

public class AirAllCityHandler {
//	private	static QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());
	private	static QueryRunner qr=new QueryRunner(DataSourceUtils.getDatasSource());
	/**
	 * 转换成json代码
	 * @param json
	 * @return
	 */
	private static final Logger log = Logger.getLogger(AirAllCityHandler.class.getName());
 	private static  List<AirInfo> readjson(String json) {
 		List<AirInfo> arr=null;
 		try {
 			arr = JSONArray.parseArray(json, AirInfo.class);
 		
		} catch (Exception e) {
			// TODO: handle exception
			log.info("数据解析错误"+e.getMessage());
		}
	
		return arr;
	}
	
 	
 	public static void saveAllCity2db(){
   		 StringBuilder sb=new StringBuilder();
   		sb.append("insert into d_airqulity_allcity ")
   		  .append("(aqi,area,co,co_24h,no2,no2_24h,o3,o3_24h,o3_8h,o3_8h_24h,")
   		 .append("pm10,pm10_24h,pm2_5,pm2_5_24h,quality,level,so2,so2_24h,")
   		 .append("primary_pollutant,time_point,order_air,id)")
   		 .append("values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
   		 .append(" ON DUPLICATE KEY UPDATE   ")
   		 .append(" aqi=?");//如果主键相同更新数据
   		 
   	List<AirInfo> arr = readjson(NetData.getStringData("http://www.pm25.in/api/querys/aqi_ranking.json?city=ankang&token=x7KbGxeVnqmwXwV8zXAB&stations=no"));
//   		List<AirInfo> arr =readjson(readfile());
//   	站点
//   	http://www.pm25.in/api/querys/station_names.json?city=baoji&token=x7KbGxeVnqmwXwV8zXAB&stations=no
   		
//   	获取一个城市所有监测点数据 如果=no只返回本市
//   	http://www.pm25.in/api/querys/aqi_details.json?city=baoji&token=x7KbGxeVnqmwXwV8zXAB&stations=yes
   		if(arr==null){
   			log.info("没有获取到数据……");
   			return;
   		}else{
   			log.info(arr.toString());
   		}
   		AirInfo ai=null;
   		try {
				Object[][] params=new Object[arr.size()][23];
				for (int i= 0; i < arr.size(); i++) {
					ai=arr.get(i);
//					if(StringUtils.isEmpty(ai.getPosition_name())) return;
					params[i][0]=ai.getAqi();
					params[i][1]=ai.getArea();
					params[i][2]=ai.getCo();
					params[i][3]=ai.getCo_24h();
					params[i][4]=ai.getNo2();
					params[i][5]=ai.getNo2_24h();
					params[i][6]=ai.getO3();
					params[i][7]=ai.getO3_24h();
					params[i][8]=ai.getO3_8h();
					params[i][9]=ai.getO3_8h_24h();
					params[i][10]=ai.getPm10();
					params[i][11]=ai.getPm10_24h();
					params[i][12]=ai.getPm2_5();
					params[i][13]=ai.getPm2_5_24h();
					params[i][14]=ai.getQuality();
					params[i][15]=ai.getLevel();
					params[i][16]=ai.getSo2();
					params[i][17]=ai.getSo2_24h();
					params[i][18]=ai.getPrimary_pollutant();
					params[i][19]=ai.getTime_point();
					params[i][20]=i+1;
					params[i][21]=ai.getTime_point()+ai.getArea();
					params[i][22]=ai.getAqi();
				} 
				qr.batch(sb.toString(), params);
			} catch (SQLException e) {
				 e.printStackTrace();
			}
 	}
 public static String readfile() {
		FileInputStream inputStream = null;
		String josn = null;
		try {
			inputStream = new FileInputStream(new File("D:\\deverlopsoft\\java\\workspace\\b_d_h\\src\\com\\base_data_handler\\air\\aqi_ranking.json"));
			int i = inputStream.available();
			byte data[] = new byte[i];
			inputStream.read(data);
			josn = new String(data,"utf-8");
			inputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return josn;
	}
 

 public static void main(String[] args) {
	 saveAllCity2db();
	
}
}
